<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="600" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
  <!-- 设备 -->
  <deviceSelectModel @register="deviceRegModal" @getSelectResult="devicehandleSuccess" v-bind="getBindValue"></deviceSelectModel>
</template>

<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner,useModal } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { transferOrderData } from '../BusProductionPlan.data';
import { saveTransfer } from '../BusProductionPlan.api';
import deviceSelectModel from '/@/views/components/deviceSelectModel.vue';

// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 80,
  schemas: transferOrderData,
  showActionButtonGroup: false,
});
//表单赋值
const setDate = ref()
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  setDate.value = data.record
  //重置表单
  await resetFields();
  setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
    await setFieldsValue({
      ...data.record,
    });
  // }
  // 隐藏底部时禁用整个表单
  setProps({ disabled: !data?.showFooter });
});
//设置标题
const title = '工单转单';
//表单提交事件
async function handleSubmit(v) {
  try {
    let values = await validate();
    setModalProps({ confirmLoading: true });
    //提交表单
    values.id = setDate.value.id
    await saveTransfer(values, isUpdate.value);
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
const getBindValue = Object.assign({}, '');
const [deviceRegModal, { openModal: openDeviceModal }] = useModal();
//设备
function deviceOpen() {
  openDeviceModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
//设备回写
function devicehandleSuccess(val, values) {
  setFieldsValue({
    deviceName: val[0].label,
    deviceId: val[0].value,
    deviceCode:val[0].item.code,
    spec:val[0].item.spec
  });
}
</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
:deep(.ant-input-number) {
  width: 100%;
}

:deep(.ant-calendar-picker) {
  width: 100%;
}
:deep(.ant-col-xs-24) {
  max-width : 100%;
}
</style>